package com.wpx.fileSys.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

import com.wpx.fileSys.dao.FileDao;
import com.wpx.fileSys.dao.FindDao;
import com.wpx.fileSys.dao.impl.FileDaoImpl;
import com.wpx.fileSys.dao.impl.FindDaoImpl;
import com.wpx.fileSys.utils.Token;

/**
 * Servlet implementation class testServlet
 */
@WebServlet("/testServlet")
public class testServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public testServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json; charset=utf-8");	

		request.setCharacterEncoding("UTF-8");
		
		PrintWriter out = null;	
		out = response.getWriter();
		String username=(String) request.getSession().getAttribute("username");
		String token = (String) request.getSession().getAttribute("token");
		Token tokenTool=new Token();
		JSONObject result=new JSONObject();
		result.put("msg", "0");
		if (!tokenTool.verifyToken(username, token)) {
			result.put("msg", "登录过期");
			out.write(result.toString());
			return;
		}
		
		
		
		FindDao findDao=new FindDaoImpl();
		JSONArray filelist=findDao.searchFiles(username, "");

		result.put("list",filelist);
		out.write(result.toString());
		
		out.flush();
	    out.close();

		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
